package learn.contorlller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import learn.bean.User;
import learn.service.UserServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Author yxc
 * @Date 2022/1/10
 */
@RestController
@RequestMapping("/mpTest")
public class MybatisPlusTestController {

    @Autowired
    private UserServiceI userServiceI;

    @RequestMapping("/insertUser")
    public boolean insertUser() {
        User user = new User();
        user.setId("11111");
        user.setAge("11");
        return userServiceI.save(user);
    }

    @RequestMapping("/listUser")
    public String listUser(@RequestParam("curr") int curr) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        List<User> list = userServiceI.list();
        Page<User> page = new Page<>(curr, 2);
        Page<User> pageUsers = userServiceI.page(page);
        String names = "";
        for (User user : pageUsers.getRecords()) {
            names += user.getUserName() + " ";
        }
        ;
        return names;
    }

    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable String id) {
        return userServiceI.removeById(id) ? "success" : "fail";
    }
}
